package cn.lan.generator.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.lan.generator.bean.EntityAttrInfo;
import cn.lan.generator.bean.EntityInfo;
import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.CreateFile;
import cn.lan.generator.utils.ConfigUtil;
import cn.lan.generator.utils.Constant;
import cn.lan.generator.utils.FileUtil;
import cn.lan.generator.utils.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建ViewController
 * @author lanping
 * @version 1.0
 * @date 2021-02-27
 **/
public class CreateViewControllerImpl implements CreateFile {

    /**
     * 创建Controller文件
     *
     * @param entityInfo 实体信息
     * @param projInfo   项目信息
     * @throws Exception
     */
    @Override
    public void writeFile(EntityInfo entityInfo, ProjInfo projInfo) throws Exception {

        String createTime = "create_time";

        String tableComment = entityInfo.getTableComment();
        String lowerPrimaryKey = StringUtil.toLowerCaseFirstOne(entityInfo.getPrimaryKey());
        String upperPrimaryKey = StringUtil.toUpperCaseFirstOne(entityInfo.getPrimaryKey());

        String upperEntityName = StringUtil.toUpperCaseFirstOne(entityInfo.getEntityName());
        String lowerEntityName = StringUtil.toLowerCaseFirstOne(entityInfo.getEntityName());

        String sortName = "";
        String sortOrder = "";
        List<EntityAttrInfo> entityAttrs = entityInfo.getEntityAttrs();
        boolean flag = false;
        for (EntityAttrInfo entityAttrInfo:entityAttrs){
            if(createTime.indexOf(entityAttrInfo.getColumnName().toLowerCase())!=-1){
                sortName = entityAttrInfo.getColumnName();
                sortOrder = "desc";
                flag = true;
                break;
            }
            if(entityAttrInfo.getColumnName().toLowerCase().indexOf("time")!=-1){
                sortName = entityAttrInfo.getColumnName();
                sortOrder = "desc";
                flag = true;
            }
        }
        if(flag==false){
            sortName = entityAttrs.get(0).getColumnName();
            sortOrder = "asc";
        }

        String packageName = ConfigUtil.getViewControllerPackage(projInfo.getPackageName());
        String entityPackage = ConfigUtil.getEntityPackage(projInfo);
        String entityListDtoPackage = ConfigUtil.getListDtoPackage(projInfo);
        String utilsPackage = ConfigUtil.getCommonUtilsPackage(projInfo.getPackageName());
        String commonBeanPackage = ConfigUtil.getCommonBeanPackage(projInfo.getPackageName());
        String controllerPackage = ConfigUtil.getControllerPackage(projInfo.getPackageName());
        String accessBaseUrl = "/"+projInfo.getProjectSimpleName()+"Web"+projInfo.getAccessBaseUrl();
        String srcFilePath = FileUtil.getResourceRootDir(projInfo.isDebugModel())+Constant.TEMPLATE_VIEW_CONTROLLER_PATH;

        String suffixPath = packageName.replaceAll("\\.", "\\\\") + "\\" + upperEntityName + "ViewController" + Constant.FILE_SUFFIX_JAVA;
        String destFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getWebServer(),  Constant.PathType.basePath, suffixPath);
        String sortNameStr = sortName;
        String sortOrderStr = sortOrder;
        Map<String,String> map = new HashMap<String,String>(){
            {
                put("filePackageName",packageName);
                put("entityPackage",entityPackage);
                put("controllerPackage",controllerPackage);
                put("upperEntityName",upperEntityName);
                put("entityListDtoPackage",entityListDtoPackage);
                put("tableComment",tableComment);
                put("projectAuthor",projInfo.getAuthor());
                put("nowDate", DateUtil.today());
                put("lowerEntityName",lowerEntityName);
                put("upperPrimaryKey",upperPrimaryKey);
                put("lowerPrimaryKey",lowerPrimaryKey);
                put("utilsPackage",utilsPackage);
                put("commonBeanPackage",commonBeanPackage);
                put("accessBaseUrl",accessBaseUrl);
                put("sortName",sortNameStr);
                put("sortOrder",sortOrderStr);
            }
        };
        FileUtil.copyFile(projInfo,srcFilePath, destFilePath, map);
    }
}
